// import { reqGetAllSchool } from "@/api/reqSchool";
// import request from "@/api/request";
// import type { RESPONSE } from "@/types/base";
// import type { UploadFile } from "element-plus";
// import { ref } from "vue";

import request from "@/api/request";
import type { RESPONSE } from "@/types/base";
import type { UploadFile } from "element-plus";
import { ref } from "vue";

// export const Upload = () => {
//   const dialogImageUrl = ref("");
//   const dialogVisible = ref(false);
//   const disabled = ref(false);

//   const fileList = ref<UploadFile[]>([]);

//   const succcessUpload = ({
//     data: { name, url },
//   }: RESPONSE<{ name: string; url: string }>,f:UploadFile) => {
//     f.name=name;
//     f.url=url;
//   };

//   const handleRemove = async (f: UploadFile) => {
//     console.log(f);
//     await request.post("/manage/img/delete", { name: f.name });

//     fileList.value = fileList.value.filter((item) => item !== f);
//   };

//   const handlePictureCardPreview = (file: UploadFile) => {
//     dialogImageUrl.value = file.url!;
//     dialogVisible.value = true;
//   };

//   const handleDownload = (file: UploadFile) => {
//     console.log(file);
//     const a = document.createElement("a");
//     a.href = file.url!;
//     a.download = file.name;
//     document.body.appendChild(a);
//     a.click();
//     document.body.removeChild(a);
//   };

//   return {
//     dialogImageUrl,
//     dialogVisible,
//     disabled,
//     handleRemove,
//     handlePictureCardPreview,
//     handleDownload,
//     succcessUpload,
//     fileList,
//   };
// };

export const Upload = () => {
  const dialogImageUrl = ref("");
  const dialogVisible = ref(false);
  const disabled = ref(false);

  const fileList = ref<UploadFile[]>([]);
  const handleRemove = async (file: UploadFile) => {
    console.log(file);
    await request.post("/manage/img/delete", {
      name: file.name,
    });

    fileList.value = fileList.value.filter((item) => item !== file);

    
  };

  const handlePictureCardPreview = (file: UploadFile) => {
    dialogImageUrl.value = file.url!;
    dialogVisible.value = true;
  };

  const handleDownload = (file: UploadFile) => {
    console.log(file);
    const a = document.createElement("a");
    a.href = file.url!;
    a.download = file.name;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  };

  const succcessUpload = (
    { data: { name, url } }: RESPONSE<{ name: string; url: string }>,
    f: UploadFile
  ) => {
    f.name = name;
    f.url = url;
  };

  return {
    dialogImageUrl,
    dialogVisible,
    disabled,
    handleRemove,
    handlePictureCardPreview,
    handleDownload,
    succcessUpload,
    fileList,
  };
};
